package com.itheima.myiotest1;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Test2 {
    public static void main(String[] args) {

        String familyName = HttpUtil.get("http://www.baijiaxing.net.cn/");
        String boyName = HttpUtil.get("http://www.cimingwang.com/nanhaimingzi/948.html");
        String girlName = HttpUtil.get("https://qiming.hao86.com/view/141985/");

        ArrayList<String> tempFamilyNamelist = (ArrayList<String>) ReUtil.findAll("([\\u4E00-\\u9FA5]{4})(，|。)", familyName, 1);
        ArrayList<String> boyNameList = (ArrayList<String>) ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(、)", boyName, 1);
        ArrayList<String> girlNameList = (ArrayList<String>) ReUtil.findAll("(、)([\\u4E00-\\u9FA5]{2})", girlName, 2);

        //将姓氏拆开
        ArrayList<String> familyNameList = new ArrayList<>();
        for (String s : tempFamilyNamelist) {
            char[] charArray = s.toCharArray();
            for (char c : charArray) {
                familyNameList.add(c + "");
            }
        }

        ArrayList<String> result = zuHe(familyNameList, boyNameList, girlNameList, 20, 20);
        Collections.shuffle(result);

        FileUtil.writeLines(result,"a.txt", Charset.forName("utf-8"));
    }

    private static ArrayList<String> zuHe(ArrayList<String> familyNameList, ArrayList<String> boyNameList, ArrayList<String> girlNameList, int boyCount, int girlCount) {

        //返回 姓名-性别-年龄
        //最终的集合
        ArrayList<String> result = new ArrayList<>();

        //随机生成年龄
        Random r = new Random();

        //打乱顺序
        Collections.shuffle(familyNameList);
        Collections.shuffle(boyNameList);
        Collections.shuffle(girlNameList);

        //随机生成姓名
        for (int i = 0; i < boyCount; i++) {
            String s = familyNameList.get(i) + boyNameList.get(i) + "-男-" + (r.nextInt(7) + 18);
            //去重
            if (result.contains(s)) {
                continue;
            }
            result.add(s);
        }

        for (int i = 0; i < girlCount; i++) {
            String s = familyNameList.get(i) + boyNameList.get(i) + "-女-" + (r.nextInt(7) + 18);
            if (result.contains(s)) {
                continue;
            }
            result.add(s);
        }

        return result;
    }
}
